//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.quickserver.net.qsadmin.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class PropertiePanel extends JPanel {
    private QSAdminMain qsadminMain;
    private JPanel targetPanel;
    private JPanel commandPanel;
    private JLabel targetLabel;
    private JRadioButton serverButton;
    private JRadioButton selfButton;
    private JButton reloadButton;
    private ButtonGroup group;
    private PropertieSet propertieSet;
    private String target = "server";
    private GridBagConstraints gbc;

    public PropertiePanel(final QSAdminMain qsadminMain) {
        this.qsadminMain = qsadminMain;
        this.targetPanel = new JPanel();
        this.targetLabel = new JLabel("<html><font style=\"font-size:10pt;color:#535353\"> <b>Target : </b></font>", 2);
        this.serverButton = new JRadioButton("Server");
        this.serverButton.setMnemonic(83);
        this.serverButton.setActionCommand("server");
        this.serverButton.setSelected(true);
        this.selfButton = new JRadioButton("Admin Server");
        this.selfButton.setMnemonic(65);
        this.selfButton.setActionCommand("self");
        this.group = new ButtonGroup();
        this.group.add(this.serverButton);
        this.group.add(this.selfButton);

        class RadioListener implements ActionListener {
            RadioListener() {
            }

            public void actionPerformed(ActionEvent e) {
                PropertiePanel.this.setTarget(e.getActionCommand());
            }
        }

        RadioListener rListener = new RadioListener();
        this.serverButton.addActionListener(rListener);
        this.selfButton.addActionListener(rListener);
        this.propertieSet = PropertieSet.getPropertieSet();
        final List list = this.propertieSet.getList();
        this.reloadButton = new JButton("Reload Properties For the Target");

        class ReloadListener implements ActionListener {
            ReloadListener() {
            }

            public void actionPerformed(ActionEvent e) {
                Thread performer = new Thread(new Runnable() {
                    public void run() {
                        Iterator cmdIt = list.iterator();
                        Propertie propertie = null;

                        while(cmdIt.hasNext()) {
                            propertie = (Propertie)cmdIt.next();
                            propertie.load(PropertiePanel.this, qsadminMain);
                        }

                        PropertiePanel.this.updateConnectionStatus(true);
                    }
                }, "QsAdminGUI-ReloadThread");
                performer.start();
            }
        }

        ReloadListener reloadListener = new ReloadListener();
        this.reloadButton.addActionListener(reloadListener);
        this.targetPanel.add(this.targetLabel);
        this.targetPanel.add(this.serverButton);
        this.targetPanel.add(this.selfButton);
        this.targetPanel.add(this.reloadButton);
        this.commandPanel = new JPanel();
        GridBagConstraints gbc = new GridBagConstraints();
        this.commandPanel.setLayout(new GridBagLayout());
        gbc.insets = new Insets(0, 0, 0, 0);
        Iterator cmdIt = list.iterator();
        Propertie propertie = null;

        while(cmdIt.hasNext()) {
            propertie = (Propertie)cmdIt.next();
            propertie.addToPanel(this.commandPanel, gbc, this, qsadminMain);
        }

        this.setLayout(new BorderLayout(0, 10));
        this.add(this.targetPanel, "North");
        this.add(this.commandPanel, "Center");
    }

    public String getTarget() {
        return this.target;
    }

    private void setTarget(String target) {
        this.target = target;
    }

    public void updateConnectionStatus(boolean connected) {
        List list = this.propertieSet.getList();
        Iterator cmdIt = list.iterator();
        Propertie propertie = null;

        while(cmdIt.hasNext()) {
            propertie = (Propertie)cmdIt.next();
            if (propertie.getSaveButton() != null) {
                propertie.getSaveButton().setEnabled(false);
            }
        }

        this.reloadButton.setEnabled(connected);
        this.serverButton.setEnabled(connected);
        this.selfButton.setEnabled(connected);
    }
}
